package zhishu.iceyyy.com.kd_khd.activity;

import android.app.AlertDialog;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import zhishu.iceyyy.com.kd_khd.R;
import zhishu.iceyyy.com.kd_khd.fragment.FL_Fragment;
import zhishu.iceyyy.com.kd_khd.fragment.GWC_Fragment;
import zhishu.iceyyy.com.kd_khd.fragment.My_Fragment;
import zhishu.iceyyy.com.kd_khd.fragment.SY_Fragment;

public class MainActivity extends FragmentActivity {

    AlertDialog.Builder alert;
    // 定义FragmentTabHost对象
    private FragmentTabHost mTabHost;
    // 定义一个布局
    private LayoutInflater layoutInflater;
    Timer timer = new Timer();
    private Class[] fragmentArray = null;
    private int[] mImageViewArray = null;
    private String[] mTextviewArray = null;
    private static Boolean isQuit = false;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintab);

        initView();


    }

    /**
     * 初始化组件
     */
    private void initView() {
        // 实例化布局对象
        layoutInflater = LayoutInflater.from(this);
        // 实例化TabHost对象，得到TabHost
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.getTabWidget().setDividerDrawable(null);
        fragmentArray = new Class[]{SY_Fragment.class, FL_Fragment.class,GWC_Fragment.class, My_Fragment.class};
        //定义数组来存放按钮图片
        mImageViewArray =
                new int[]{R.drawable.tab_sy_btn, R.drawable.tab_tk_btn,R.drawable.tab_gwc_btn, R.drawable.tab_wd_btn
                };
        //Tab选项卡的文字
        mTextviewArray = new String[]{"首页", "分类", "购物车", "我的" };

        // 得到fragment的个数
        int count = fragmentArray.length;

        for (int i = 0; i < count; i++) {
            // 为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
                    .setIndicator(getTabItemView(i));
            // 将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
            // 设置Tab按钮的背景
            mTabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.color.white);
        }








    }



    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(int index) {
        View view = layoutInflater.inflate(R.layout.tab_item_1_view, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(mImageViewArray[index]);

        TextView textView = (TextView) view.findViewById(R.id.textview);
        textView.setText(mTextviewArray[index]);

        return view;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isQuit == false) {
                isQuit = true;
                Toast.makeText(getBaseContext(), "再按一次返回键退出程序",
                        Toast.LENGTH_SHORT).show();
                TimerTask task = null;
                task = new TimerTask() {
                    public void run() {
                        isQuit = false;
                    }
                };
                timer.schedule(task, 2000);
            } else {

                finish();

            }
        }
        return false;
    }
}
